package com.catt.orderdinner.controller;

import com.catt.orderdinner.common.KeyUtils;
import com.catt.orderdinner.dataobject.TbFood;
import com.catt.orderdinner.dataobject.TbShop;
import com.catt.orderdinner.form.FoodForm;
import com.catt.orderdinner.service.FoodService;
import com.catt.orderdinner.service.ShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin/food")
public class AdminFoodController {

    @Autowired
    private FoodService foodService;

    @Autowired
    private ShopService shopService;

    @RequestMapping("list")
    public String getfoodList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "size", defaultValue = "2") Integer size, Model model){
        PageRequest request = new PageRequest(page - 1, size);
        Page<TbFood> allFood = foodService.findAll(request);
        model.addAttribute("pageUrl","/admin/food/list");
        model.addAttribute("currentPage",page);
        model.addAttribute("pageInfo",allFood);
        model.addAttribute("size",size);
        return "food/foodList";
    }

    @GetMapping("showForm")
    public String showForm( String id,Model model){
        if (id!=null&&!id.equalsIgnoreCase("")) {
            TbFood food = foodService.findOneByFoodId(id);
            model.addAttribute("food",food);
        }
        List<TbShop> shopList = shopService.findAll();
        model.addAttribute("shopList",shopList);
        return "food/foodForm";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public String saveAndUpdate(@Valid FoodForm form, String id, Model model){
        TbFood Tfood = new TbFood();
        //如果foodId为空, 说明是新增
        if (!StringUtils.isEmpty(form.getFoodId())) {
            Tfood = foodService.findOneByFoodId(form.getFoodId());
        }else {
            form.setFoodId(KeyUtils.genUniqueKey());
        }
        BeanUtils.copyProperties(form, Tfood);
        try {
            foodService.save(Tfood);
            return "操作成功";
        }catch (Exception e){
            return "操作失败";
        }
    }

    @RequestMapping("del")
    @ResponseBody
    public String del(String ids){
        try {
            String[] idss = ids.split("-");
            for(String id : idss){
                foodService.del(id);
            }
            return "success";
        }catch (Exception e){
            return "failed";
        }
    }
}
